<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{path:'/home'}">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddDialog">添加产品</el-button>
        </el-col>
      </el-row>
      <table-tree
        class="treeTable"
        :data="cateList"
        :columns="columns"
        show-index
        index-text="#"
        show-row-hover
        border
        :selection-type="false"
        :expand-type="false"
      >
        <template slot="isOk" slot-scope="scope">
          <i
            v-if="scope.row.cat_deleted === false"
            style="color:lightgreen"
            class="el-icon-success"
          ></i>
          <i v-else class="el-icon-error" style="color:red "></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag size="mini" v-else-if="scope.row.cat_level === 2" type="warning">三级</el-tag>
        </template>
        <template slot="opretion">
          <el-button type="primary" size="mini">
            <i class="el-icon-edit"></i>编辑
          </el-button>
          <el-button type="danger" size="mini">
            <i class="el-icon-delete"></i>删除
          </el-button>
        </template>
      </table-tree>
      <el-pagination
        :total="total"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        :page-sizes="[1,5,10]"
        layout="total,sizes,prev,next,pager,jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      ></el-pagination>
    </el-card>
    <el-dialog title="添加分类" :visible.sync="isAddDialog" width="60%" @close="colseAddDialog">
      <el-form label-width="80px" ref="addCateForm" :model="addCateForm" :rules="addCateFormRule">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop>
          <el-cascader
            v-model="selectedParent"
            :options="parentCateList"
            @change="changedParentCate"
            :props="cascaderProps"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isAddDialog = false">取消</el-button>
        <el-button type="primary" @click="addClassify">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 获取商品分类请求数据
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类数据列表
      cateList: [],
      //   商品分类总条数
      total: 0,
      // 是否显示添加分类对话框
      isAddDialog: false,
      // =选中的父级分类
      selectedParent: [],
      // 父级分类
      parentCateList: [],
      // 表单数据
      addCateForm: {
        // 分类名称
        cat_name: '',
        // 父级id
        cat_pid: '',
        // 等级，默认为0
        cat_level: 0
      },
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      },
      // 表单验证规则
      addCateFormRule: {
        cat_name: [
          {
            required: true,
            message: '商品分类不能为空',
            trigger: 'blur'
          }
        ]
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opretion'
        }
      ]
    }
  },
  //   生命周期------创建之后
  created() {
    this.getCategories()
  },
  methods: {
    changedParentCate() {
      console.log(this.selectedParent)
    },
    // 获取父级分类
    async getParentCateGories() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类失败')
      }
      this.parentCateList = res.data
    },
    // 获取商品分类数据
    async getCategories() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 点击确定按钮添加分类
    addClassify() {
      this.$refs.addCateForm.validate(validate => {
        if (!validate) return
        let length = this.selectedParent.length
        if (length === 2) {
          this.addCateForm.cat_pid = this.selectedParent[1]
        } else if (length === 1) {
          this.addCateForm.cat_pid = this.selectedParent[0]
        } else {
          this.addCateForm.cat_pid = 0
        }
        this.addCateForm.cat_level = length
        this.Axios_addCate()
      })
    },
    async Axios_addCate() {
      const { data: res } = await this.$http.post(
        'categories',
        this.addCateForm
      )
      console.log(res.meta.status)
      if (res.meta.status !== 201) {
        return this.$message.error('添加失败')
      }
      this.$message.success('添加成功')
      this.getCategories()
      this.isAddDialog = false
    },
    // 显示添加分类对话框
    showAddDialog() {
      this.isAddDialog = true
      this.getParentCateGories()
    },
    // 关闭添加分类对话框
    colseAddDialog() {
      this.addCateForm = {
        cat_name: '',
        // 父级id
        cat_pid: '',
        // 等级，默认为0
        cat_level: 0
      }
      this.selectedParent = []
    },
    // 点击分页进行翻页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCategories()
    },
    // 每页条数发生改变
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getCategories()
    }
  }
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
